<template>
  <div class="app-container">
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <label class="el-form-item-label">料号</label>
        <el-input v-model="query.materialCode" clearable placeholder="料号" style="width: 185px;" class="filter-item" @keyup.enter.native="toQuery" />
        <label class="el-form-item-label">批次号</label>
        <el-input v-model="query.materialBatch" clearable placeholder="批次号" style="width: 185px;" class="filter-item" @keyup.enter.native="toQuery" />
        <label class="el-form-item-label">状态</label>
        <el-select v-model="query.status" class="filter-item" style="width: 185px;" placeholder="请选择状态" @change="toQuery">
          <el-option
            v-for="item in dict.semi_material_status"
            :key="item.value"
            :label="item.label"
            :value="Number(item.value)"
          />
        </el-select>
        <el-button type="success" icon="el-icon-search" size="mini" class="filter-item" @click="toQuery">搜索</el-button>
        <el-button type="warning" icon="el-icon-refresh-right" size="mini" class="filter-item" @click="reset">重置</el-button>
      </div>
      <!-- 模态框 -->
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="500px"
        :close-on-click-modal="false"
        :before-close="handleClose"
      >
        <el-form :model="form" size="small" label-width="120px">
          <el-form-item label="料号">
            <el-input v-model="form.materialCode" style="width: 250px;" readonly />
          </el-form-item>
          <el-form-item label="批次号">
            <el-input v-model="form.materialBatch" style="width: 250px;" readonly />
          </el-form-item>
          <el-form-item label="更新状态">
            <el-select v-model="form.status" style="width: 250px;" placeholder="请选择">
              <el-option
                v-for="item in dict.semi_material_status"
                :key="item.value"
                :label="item.label"
                :value="Number(item.value)"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" :loading="btn.update" @click="confirmUpdate">确 定</el-button>
        </span>
      </el-dialog>
      <el-table v-loading="loading" :data="list" style="width: 100%">
        <el-table-column prop="materialCode" label="料号" />
        <el-table-column prop="materialBatch" label="批次号" />
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{ dict.label.semi_material_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="checkCompleteTime" label="检验完成时间" />
        <el-table-column label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="handleEdit(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :page-size.sync="query.size"
        :total="total"
        :current-page.sync="query.current"
        style="margin-top: 8px;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChangeHandler"
        @current-change="pageChangeHandler"
      />
    </div>
  </div>
</template>

<script>
import { queryInfoAPI, updateStatusAPI } from '@/api/jtWork/jtQualityManagement'
export default {
  name: 'JtQualityManagement',
  dicts: ['semi_material_status'],
  data() {
    return {
      query: {
        materialCode: '',
        materialBatch: '',
        status: '',
        current: 1,
        size: 10
      },
      total: 0,
      list: [],
      loading: false,
      dialogVisible: false,
      form: {},
      btn: {
        update: false
      }
    }
  },
  mounted() {
    this.toQuery()
  },
  methods: {
    toQuery() {
      this.query.current = 1
      this.getList()
    },
    async getList() {
      this.loading = true
      try {
        const res = await queryInfoAPI(this.query)
        // console.log(res)
        if (res.data.totalElements > 0) {
          this.list = res.data.content
          this.total = res.data.totalElements
        }
      } finally {
        this.loading = false
      }
    },
    reset() {
      this.query = {
        materialCode: '',
        materialBatch: '',
        status: '',
        current: 1,
        size: 10
      }
      this.getList()
    },
    pageChangeHandler(e) {
      this.query.current = e
      this.getList()
    },
    // 每页条数改变
    sizeChangeHandler(e) {
      this.query.size = e
      this.query.current = 1
      this.getList()
    },
    handleEdit(row) {
      const rowJson = JSON.stringify(row)
      const newRow = JSON.parse(rowJson)
      this.dialogVisible = true
      this.form = newRow
    },
    handleClose() {},
    async confirmUpdate() {
      try {
        this.btn.update = true
        const res = await updateStatusAPI(this.form)
        if (res.code === 200) {
          this.$message.success('修改成功')
          this.dialogVisible = false
          this.toQuery()
        } else {
          this.$message.warning(res.message)
          this.dialogVisible = false
        }
      } finally {
        this.btn.update = false
      }
    }
  }
}
</script>

<style scoped lang="scss">
</style>
